package com.mall.controller.protal;

import com.mall.common.Const;
import com.mall.common.MySessionContext;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.Shipping;
import com.mall.pojo.User;
import com.mall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by xiaoyao on 2017/8/30.
 */
@Controller
@RequestMapping("/shipping/")
public class ShippingController {

    @Autowired
    private IShippingService iShippingService;


    /**
     * 新增收货地址
     * @param shipping
     * @return
     */
    @RequestMapping("add.do")
    @ResponseBody
    public ServerResponse add(Shipping shipping){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iShippingService.add(user.getId(),shipping);
    }

    /**
     * 删除收货地址
     * @param shippingId
     * @return
     */
    @RequestMapping("delete.do")
    @ResponseBody
    public ServerResponse delete(Integer shippingId){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iShippingService.delete(user.getId(),shippingId);
    }

    /**
     * 更新地址
     * @param shipping
     * @return
     */
    @RequestMapping("update.do")
    @ResponseBody
    public ServerResponse update(Shipping shipping){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iShippingService.update(user.getId(),shipping);
    }

    /**
     * 查询地址
     * @param shippingId
     * @return
     */
    @RequestMapping("select.do")
    @ResponseBody
    public ServerResponse select(Integer shippingId){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iShippingService.select(user.getId(),shippingId);
    }


    /**
     * 地址列表，分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse list(
                               @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iShippingService.list(user.getId(),pageNum,pageSize);
    }



}
